home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr10 / v12n12.zip / PCMCVT.ZIP / PCMASM.ZIP / SEARCH.INC < prev    next >
Text File  |  1993-05-22  |  3KB  |  74 lines

  1. ;SEARCH.INC
  2. ;Copyright 1993 (c) Jay Munro
  3. ;Based on algorithm in CSInstr from Crescent Software
  4. ;SearchString compares two strings, returning position in AX if match.  If
  5. ;   a partial match occurs, then the partial position is in BX
  6.  
  7. SearchString Proc Near Uses ES DI, Strt:Word, SrcStr:Word, SLen:Word, Search:Word, SrcLen:Word
  8.     Cld                   ;forward moves on LodSb
  9.     Mov  DX,Strt          ;get the address for StrStr
  10.     Dec  DX               ;adjust StrStr so 1st character = 0 offset
  11.     Or   DX,DX            ;make sure we didn't pass a zero length
  12.     Js   NotFound         ;no, get out and assign SearchString = 0
  13.     Mov  CX,SLen          ;put LEN(SrcStr) into CX
  14.     Jcxz NotFound         ;it's null, get out and assign SearchString = 0
  15.     Sub  CX,DX            ;consider only remaining length of SrcStr
  16.     Jle  NotFound         ;not enough remains, it couldn't be in there
  17.     Mov  BX,SrcStr        ;now BX points to the beginning of SrcStr
  18.  
  19.     Mov  SI,BX            ;set SI to point to the beginning of SrcStr
  20.     Add  SI,DX            ;now SI points to the correct Strt offset
  21.     Mov  ES,BX            ;remember SrcStr Starting address in ES
  22.     Xor  BX,BX            ;set up BX for exit in case Search is null
  23.     Mov  DX,SI            ;remember where we are in SrcStr, in case
  24.                           ;  a false partial match takes us on a wild
  25.                           ;  goose chase - lets us resume in SrcStr 
  26.     Mov  DI,Search        ;now DI points to the beginning of Search
  27.     Mov  BP,SrcLen        ;put LEN(Search) into BP
  28.     Or   BP,BP            ;is Search null?
  29.     Jz   Found            ;yes, get out and assign SearchString = Strt
  30.     Jmp Short StrtScan
  31.  
  32. Reset:
  33.     Inc  DX               ;point to next character in SrcStr
  34.     Mov  SI,DX            ;point SI to the next character too
  35.     Dec  CX               ;consider only remaining length of SrcStr
  36.  
  37. StrtScan:
  38.     Xor  BX,BX            ;zero out BX as an index into Search
  39.     Cmp  BP,CX            ;is the rest of Search longer than SrcStr?
  40.     Jg   NotFoundP        ;yes, it couldn't possibly be in there
  41.  
  42. Okay:
  43.     Cmp  BX,BP            ;did we exhaust Search?
  44.     Je   Found            ;yes, and if we made it this far it's there
  45.  
  46.     Mov  AH,[DI+BX]       ;get the current character in Search
  47.     Inc  BX               ;point to the next character for next time
  48.  
  49.     Lodsb                 ;load the current SrcStr character into AL
  50.     Cmp  AL,AH            ;are they the same?
  51.     Jnz  Reset            ;no, consider the next character in SrcStr
  52.     Jmp  Short Okay       ;yes, continue comparing
  53.  
  54. NotFound: 
  55.     Xor  SI,SI            ;clear SI to assign AX = 0 below
  56.     Mov  BX,SI            ;clear BX too
  57.     Jmp  Short SrchExit
  58.  
  59. NotFoundP:                ;partial not found
  60.     Xor  SI,SI            ;clear SI to assign AX = 0 below
  61.     Mov  BX,-1            ;lets caller know that we ran out of string
  62.     Jmp  Short SrchExit
  63.  
  64. Found:
  65.     Mov  DX,ES            ;retrieve the original SrcStr Start address
  66.     Sub  SI,DX            ;calculate the number of characters searched
  67.     Sub  SI,BX            ;because we ended up past where it was found
  68.     Inc  SI               ;the inevitable "fudge factor"
  69.  
  70. SrchExit:
  71.     Mov  AX,SI            ;assign SearchString = SI
  72.     Ret                   ;return to caller
  73. SearchString Endp
  74.